package bma.common.netty.webserver.common;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import bma.common.netty.webserver.Session;

public class SessionCommon implements Session {

	private String id;
	private long creationTime;
	private long lastAccessedTime;
	private int maxInactiveInterval;
	private Map<String, Object> data;

	public SessionCommon(String sid) {
		this(sid, new ConcurrentHashMap<String, Object>());
	}

	public SessionCommon(String sid, Map<String, Object> data) {
		super();
		this.id = sid;
		this.data = data;
		this.creationTime = this.lastAccessedTime = System.currentTimeMillis();
	}

	@Override
	public long getCreationTime() {
		return creationTime;
	}

	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public long getLastAccessedTime() {
		return lastAccessedTime;
	}

	@Override
	public void setMaxInactiveInterval(int interval) {
		maxInactiveInterval = interval;
	}

	@Override
	public int getMaxInactiveInterval() {
		return maxInactiveInterval;
	}

	@Override
	public Object getAttribute(String name) {
		return data.get(name);
	}

	@Override
	public Set<String> getAttributeNames() {
		return data.keySet();
	}

	@Override
	public void setAttribute(String name, Object value) {
		data.put(name, value);
	}

	@Override
	public void removeAttribute(String name) {
		data.remove(name);
	}

	@Override
	public void invalidate() {
		data.clear();
	}

	@Override
	public boolean isNew() {
		return false;
	}

}
